package com.cy.pj.brand.service;

import com.cy.pj.brand.dao.BrandDao;
import com.cy.pj.brand.pojo.Brand;
import com.cy.pj.common.anno.RequiredCache;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BrandServiceImpl implements BrandService {

//    使用lombok 来自动创建Logger对象
//    private static final Logger log = LoggerFactory.getLogger(BrandServiceImpl.class);
    /*slf4j: Simple Log Facade For Java 门面模式*/

    @Autowired
    private BrandDao brandDao;

    @Override
    @RequiredCache
    public List<Brand> findBrands(String name) {
        long t1 = System.currentTimeMillis();
        List<Brand> list =  brandDao.findBrands(name);
        long t2 = System.currentTimeMillis();

        log.info("findBrands -> time ->{}", t2 - t1);

        return list;
    }

    @Override
    public Integer deleteBrandById(Integer id) {
        long t1 = System.currentTimeMillis();
        Integer rows = brandDao.deleteBrandById(id);
        long t2 = System.currentTimeMillis();

        log.info("deleteBrandById time -> {}",t2 - t1);

        return rows;
    }

    @Override
    public Integer insertBrand(Brand brand) {
        long t1 = System.currentTimeMillis();
        Integer rows = brandDao.insertBrand(brand);
        long t2 = System.currentTimeMillis();

        log.info("deleteBrandById time -> {}",t2 - t1);

        return rows;
    }

    @Override
    public Integer updateBrand(Brand brand) {
        long t1 = System.currentTimeMillis();
        Integer rows = brandDao.updateBrand(brand);
        long t2 = System.currentTimeMillis();

        log.info("deleteBrandById time -> {}",t2 - t1);

        return rows;
    }

/*    @Override
        因为这个方法不能真正的修改
    public Integer updateBrandById(Integer id) {
        long t1 = System.currentTimeMillis();
        Brand brand = brandDao.findBrandById(id);
        Integer rows = brandDao.updateBrand(brand);
        long t2 = System.currentTimeMillis();

        log.info("deleteBrandById time -> {}",t2 - t1);

        return rows;
    }*/

    @Override
    public Brand findBrandById(Integer id) {
        return brandDao.findBrandById(id);
    }
}
